﻿using MoreMountains.NiceVibrations;
using System.Collections;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using UnityEngine;

//游戏通用功能
public static class GameFunction
{
    /// <summary>
    /// 手机震动
    /// </summary>
    /// <param name="type">震动类型：0.不震动 1~3震动等级</param>
    public static void Vibrate(int type)
    {
        if (type == 0)
            return;
        switch (type)
        {
            case 1:
                MMVibrationManager.Haptic(HapticTypes.LightImpact);
                break;
            case 2:
                MMVibrationManager.Haptic(HapticTypes.MediumImpact);
                break;
            case 3:
                MMVibrationManager.Haptic(HapticTypes.HeavyImpact);
                break;
            default:
                MMVibrationManager.Haptic(HapticTypes.LightImpact);
                break;
        }
    }

    public static KeyLayoutData GetKeyLayoutData(GameObject key_real)
    {
        KeyLayoutData data = new KeyLayoutData();
        data.position = key_real.GetComponent<RectTransform>().localPosition;
        data.size = key_real.GetComponent<RectTransform>().sizeDelta;
        return data;
    }

    public static void SetKeyLayout(KeyLayoutData data, GameObject key_real,Transform parent)
    {
        key_real.transform.SetParent(parent);
        key_real.GetComponent<RectTransform>().localPosition = data.position;
        key_real.GetComponent<RectTransform>().sizeDelta = data.size;
    }

    public static void CreateKeyLayout(KeyLayoutData[] datas, GameObject key_prefab, Transform ui_root)
    {
        for (int i = 0; i < datas.Length; i++)
        {
            GameObject key_real = Object.Instantiate(key_prefab);
            SetKeyLayout(datas[i], key_real, ui_root);
        }
    }


    public static Vector2 MouseToUI(Canvas canvas)
    {
        Vector2 _pos = Vector2.one;
        RectTransformUtility.ScreenPointToLocalPointInRectangle(canvas.transform as RectTransform, Input.mousePosition, canvas.worldCamera, out _pos);
        return _pos;
    }

    public static bool IsEnglishAndNum(string c)
    {
        Regex rex = new Regex("[a-z0-9A-Z_]+");
        Match ma = rex.Match(c);
        return ma.Success;
    }
}
